package cn.smbms.controller;

import cn.smbms.pojo.User;
import cn.smbms.service.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: laoyu
 * @Date: 2019/9/2 14:18
 * @Description:
 */
@Controller
@RequestMapping("/t")
public class JsonUserController {
    @Resource
    private UserService userService;

    /**
     * 根据id查询用户信息
     * @param id
     * @return  返回json格式数据
     */
    @RequestMapping(value = "/user",
            method = RequestMethod.GET)
    @ResponseBody
    public Object getAll(@RequestParam Integer id){
        User user=null;
        //如果没有参数id
        if(null==id){
            return "nodata";
        }else{
            try {
                user = userService.queryUserById(id);
            } catch (Exception e) {
                e.printStackTrace();
                return "failed";
            }
        }
        return user;
    }










    @RequestMapping(value = "/view")
    @ResponseBody
    public Object getUserList(@RequestParam Integer id) {
//        String cjson = "";
        User user = new User();
        if (null == id) {
            return "nodata";
        } else {

            try {
//                User user = userService.queryUserById(id);
                user = userService.queryUserById(id);
//                cjson = JSON.toJSONString(user);
            } catch (Exception e) {
                e.printStackTrace();
                return "failed";
            }
        }
//        return cjson;
        return user;
    }
}
